let app = getApp();
import { getAuthentic, createAntchain,querySignFlow} from "../../../api/index";
const esignPlugin = requirePlugin("esign");
Page({
  data: {
    certName: '',
    certNo: '',
    userImg: '',
    orderId: ''
  },
  onLoad(option) {
    let userInfo = my.getStorageSync({ key: 'userInfo' }).data
    let curOrderId = my.getStorageSync({ key: 'orderId' }).data;
    this.setData({
      orderId: option.orderId || curOrderId,
      userImg: userInfo ? userInfo.avatar : ''
    })
    this.getInfo()
  },
  events: {
    onBack() {
      my.confirm({
        title: '提示',
        content: '当前订单合同签署未完成,是否继续',
        confirmButtonText: '是,继续',
        cancelButtonText: '否,回首页',
        success: res => {
          if (res.confirm) {
            my.reLaunch({ url: '../../../pages/user/orderpay/orderpay?id=' + this.data.orderId })
          } else {
            my.switchTab({ url: '../../../pages/index/index' })
          }
        },
      });
    },
  },
  onShow() {
    setTimeout(() => { my.hideLoading() }, 500);
    let hasCreateSign = my.getStorageSync({ key: 'hasCreateSign' }).data;
    if (hasCreateSign) {
      const { code } = esignPlugin.getSignStatusInfo();
      // console.log(code);
      if (code == '000') {
        querySignFlow(this.data.orderId).then(res => {
          console.log(res);
          if(!res){
            my.showToast({
              type: 'fail',
              content: '租赁宝签约发起失败',
              duration: 2000,
            });
          }
        });
        my.reLaunch({
          url: '../../pages/myOrder/myOrder?order_status=0'
        })
      }
    }
  },
  getInfo() {
    getAuthentic(this.data.orderId).then(res => {
      this.setData({
        certName: res.cert_name,
        certNo: res.cert_no
      })
    })
  },
  // 智能合同
  smartContract() {
    const that = this
    createAntchain(that.data.orderId).then(res => {
      my.setStorage({ key: 'hasCreateSign', data: true });
      my.showLoading({ content: '正在跳转合同...' });
      console.log(res);
      my.navigateTo({
        url: 'plugin://esign/esign?env=prod&flowId='+res.flowId+'&signerId='+res.signerId,
      })

      // function getSignUrl(aliSchema) {
      //   if (!aliSchema) { return '' }
      //   const querys = aliSchema.split('?')[1].split('&');
      //   const signUrlKeyValue = querys.find((item) => item.includes('query=')).replace('query=', '');
      //   const encodedSignUrl = decodeURIComponent(signUrlKeyValue).replace('signUrl=', '');
      //   return encodedSignUrl
      // }
      // const signUrl = getSignUrl(res.sign_url);
      // my.navigateToMiniProgram({
      //   appId: '2021001152620480',
      //   path: `pages/signH5/index?signUrl=${signUrl}`,
      //   success: (res) => {
      //     my.hideLoading({
      //       page: that,
      //     });
      //   },
      //   fail: (res) => {
      //     my.hideLoading({
      //       page: that,
      //     });
      //   }
      // });
    })
  },
  submitFlow() {
    this.smartContract()
  }
});
